package com.none.web.controller.beacon;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import spring.mvc.bind.annotation.FormModel;

import com.none.core.common.utils.JSONUtil;
import com.none.core.model.Pager;
import com.none.core.model.Result;
import com.none.web.common.SysConstant;
import com.none.web.model.TBeacon;
import com.none.web.service.beacon.IBeaconService;

/**
 * Beacon管理Controller
 * 
 * @author steve
 * 
 */
@Controller
@RequestMapping("beacon")
public class BeaconController {

	@Resource(name = "beaconService")
	IBeaconService beaconService;
	
	@Autowired
	private HttpServletRequest request;

	/**
	 * 添加
	 * 
	 * @param beacon
	 * @param newsId
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("addBeacon")
	public Object addBeacon(@FormModel("beacon") final TBeacon beacon, String newsId) throws Exception {
		String userId = request.getHeader("BasicAuthUsername");
		String[] filter = new String[] { "tnews", "pop_times", "out_distance", "instruction", "pop_image", "pop_times" };
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("beacon", beaconService.addBeacon(beacon, newsId,userId));
		Result result = new Result(SysConstant.STATE_SUCCESS, "beacon.addSuccess", data);
		return JSONUtil.toJSON(result, filter);
	}

	/**
	 * 修改
	 * 
	 * @param beacon
	 * @param newsId
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("editBeacon")
	public Object editBeacon(@FormModel("beacon") final TBeacon beacon, String newsId) throws Exception {
		String userId = request.getHeader("BasicAuthUsername");

		String[] filter = new String[] { "tnews", "pop_times", "out_distance", "instruction", "pop_image", "pop_times" };
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("beacon", beaconService.editBeacon(beacon, newsId,userId));
		Result result = new Result(SysConstant.STATE_SUCCESS, "beacon.editSuccess", data);
		return JSONUtil.toJSON(result, filter);
	}

	/**
	 * beacon的list
	 * 
	 * @param pager
	 * @param ibeaconId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("listBeacon")
	public Object listBeacon() {
		String[] filter = new String[] { "TBeacons", "TTagses", "tnews" };
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("beacon", beaconService.listBeacon());
		Result result = new Result(SysConstant.STATE_SUCCESS, "beacon.haveBeacon", data);
		return JSONUtil.toJSON(result, filter);
	}
	
	

	/**
	 * beacon的list
	 * 
	 * @param pager
	 * @param ibeaconId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("searchBeacon")
	public Object searchBeacon(@FormModel("pager") final Pager pager, String ibeaconId)throws Exception {
		String userId = request.getHeader("BasicAuthUsername");

		String[] filter = new String[] { "TBeacons","TTagses", "pop_times", "out_distance", "instruction", "pop_image", "pop_times","authorUser","updateUser","approvalUser"};
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("beacon", beaconService.searchBeacon(pager, ibeaconId,userId));
		data.put("pager",pager);
		Result result = new Result(SysConstant.STATE_SUCCESS, "beacon.haveBeacon", data);
		return JSONUtil.toJSON(result, filter);
	}

	/**
	 * 得到所关联的news
	 * 
	 * @param beacon
	 * @return
	 */
	@ResponseBody
	@RequestMapping("listNews")
	public Object listNews(@FormModel("beacon") final TBeacon beacon) throws Exception {
		String[] filter = new String[] { "TBeacons", "TNewses", "password", "tcodes","lastTime","activitys"};
		String userId = request.getHeader("BasicAuthUsername");
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("news", beaconService.listNews(beacon,userId));
		Result result = new Result(SysConstant.STATE_SUCCESS, "beacon.haveNews", data);
		return JSONUtil.toJSON(result, filter);
	}
	
	
	/**
	 * 删除beacon
	 * 
	 * @param beaconId
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("delBeacon")
	public Object delBeacon(String beaconId) throws Exception {
		String userId = request.getHeader("BasicAuthUsername");
 		Map<String, Object> data = new HashMap<String, Object>();
		Result result = new Result(SysConstant.STATE_SUCCESS, "beacon.delSuccess", data);
		if (!beaconService.delBeacon(beaconId,userId)) {
			result = new Result(SysConstant.STATE_ERROR, "beacon.delError", data);
		}
		return JSONUtil.toJSON(result);
	}

}
